package bx.com.smartcashier.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import bx.com.smartcashier.R;
import bx.com.smartcashier.zxing.Constants;

import static bx.com.smartcashier.application.BxApplication.addActivity;


public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
    long startTime =0;
    long endTime = 0;
    LinearLayout llSetting,llTransaction,llReturnGoods,llCashRegister;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addActivity(this);
        setContentView(R.layout.activity_home);
        initViews();
        setLisenters();
    }

    private void setLisenters() {
        llSetting.setOnClickListener(this);
        llTransaction.setOnClickListener(this);
        llReturnGoods.setOnClickListener(this);
        llCashRegister.setOnClickListener(this);
    }




    private void initViews() {
        llSetting=findViewById(R.id.ll_setting);
        llTransaction=findViewById(R.id.ll_transaction);
        llReturnGoods=findViewById(R.id.ll_return_goods);
        llCashRegister=findViewById(R.id.ll_cash_register);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            endTime = System.currentTimeMillis();
            if(endTime - startTime > 2000){
                Toast.makeText(this, "再次点击退出", Toast.LENGTH_SHORT).show();
            }else {
                finish();
            }
            startTime = System.currentTimeMillis();
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        switch(v.getId()){
            case R.id.ll_cash_register:
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    // android 6.0以上需要动态申请权限
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, Constants.REQ_PERM_CAMERA);
                    return;
                }
                intent.setClass(this,CashRegisterActivity.class);
//                intent.setClass(this,CaptureActivity.class);
                startActivityForResult(intent, Constants.REQ_QR_CODE);
            break;
            case R.id.ll_return_goods:
                intent.setClass(this,ReturnGoodsActivity.class);
                startActivity(intent);
            break;
            case R.id.ll_setting:

                intent.setClass(this,SettingActivity.class);
                startActivity(intent);

            break;
            case  R.id.ll_transaction:
                intent.setClass(this,TransactionActivity.class);
                startActivity(intent);
            break;
        }
    }
}
